package com.tjetc.service.impl;

import com.tjetc.domain.Order;
import com.tjetc.domain.Vehicle;
import com.tjetc.mapper.OrderMapper;
import com.tjetc.mapper.VehicleMapper;
import com.tjetc.service.OrderService;
import com.tjetc.utils.Db;
import com.tjetc.vo.Result;

import java.math.BigDecimal;
import java.time.temporal.ChronoUnit;
import java.util.List;

public class OrderServiceImpl implements OrderService {
    private VehicleMapper vehicleMapper = Db.getMapper(VehicleMapper.class);
    private OrderMapper orderMapper=Db.getMapper(OrderMapper.class);
    private Result result=new Result();
    @Override
    public Result commit(Order order) {
        Vehicle vehicle = vehicleMapper.selectByPrimaryKey(order.getVehicleId());
        if (vehicle == null) {
            return new Result(500, "没有此车", null);
        } else {
           /* BigDecimal hour = new BigDecimal(24);
            BigDecimal cost = vehicle.getDailyRentalRate().divide(hour, 2);
            BigDecimal hours = new BigDecimal(ChronoUnit.HOURS.between(order.getRentalStartDate(), order.getRentalEndDate()));
            order.setTotalCost(hours.multiply(cost));*/
            BigDecimal hour = new BigDecimal(24);
            BigDecimal hours = new BigDecimal(ChronoUnit.HOURS.between(order.getRentalStartDate(), order.getRentalEndDate()));
            order.setTotalCost(vehicle.getDailyRentalRate().multiply(hours.divide(hour,2)));
            int rows=orderMapper.insert(order);
            if (rows==1){
                Vehicle vehicle1 = new Vehicle();
                vehicle1.setCurrentStatus(1);
                vehicle1.setVehicleId(order.getVehicleId());
                vehicleMapper.update(vehicle1);
                return new Result(200,"提交成功",null);
            }
        }

        return new Result(500,"提交失败",null);
    }

    @Override
    public List<Order> findAll() {
        return orderMapper.findAll();
    }

    @Override
    public List<Order> toBeReviewed() {
        return orderMapper.toBeReviewed();
    }

    @Override
    public Result selectById(String orderId) {
        Order order = orderMapper.selectByPrimaryKey(orderId);
        if (order==null){
            return result.setCode(500).setMsg("请输入正确的订单号!");
        }else {
            if (order.getOrderStatus()==0){
                return result.setCode(200).setMsg("开始审核").setData(order);
            }
            else {
                return result.setCode(501).setMsg("此订单已进行审核!").setData(order);
            }
        }
    }

    @Override
    public List<Order> findByCustomerId(int id) {
        return orderMapper.findByCustomerId(id);
    }

    @Override
    public void update(String orderId, int orderStatus) {
        orderMapper.updateStatus(orderId,orderStatus);
    }

    @Override
    public List<Order> findByCustomerIdAndStatus(Integer id, int order_status) {
        return orderMapper.findByCustomerIdAndStatus(id,order_status);
    }

    @Override
    public Result delete(String orderId) {
        int rows=orderMapper.deleteByPrimaryKey(orderId);
        if (rows==1){
            return result.setCode(200).setMsg("取消成功");
        }else{
            return result.setCode(500).setMsg("取消失败");
        }
    }

    @Override
    public List<Order> findByCustomerIdAndWei(Integer id, int i) {
        return orderMapper.findByCustomerIdAndWei(id,i);
    }

    @Override
    public List<Order> findList(int i, int i1) {
        return orderMapper.findList(i,i1);
    }

}
